package com.fbw.ssm.controller;


import com.fbw.ssm.pojo.User;
import com.fbw.ssm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping(value = "/user",produces = "text/html;charset=UTF-8")
public class UserController {
    @Autowired
    private UserService userService;

    //保存
    @RequestMapping("save")
    //produce设置响应编码格式
    @ResponseBody
    public String save(User user) throws IOException {
        //直接调用user方法，spring根据传入的键名，去找对应的get/set方法
        System.out.println(user);
        userService.save(user);
        return "<h1>保存成功</h1>";
    }

    //查询
    @RequestMapping("findAll")
    public ModelAndView findAll(){
        List<User> list;
        list = userService.findAll();
        ModelAndView modelAndView =new ModelAndView();
        modelAndView.addObject("list",list);
        modelAndView.setViewName("userList");
        return modelAndView;
    }
}
